package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;

import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.UnitMapper;
import com.zhijian.warehouse.manage.entity.po.Category;
import com.zhijian.warehouse.manage.entity.po.Unit;
import com.zhijian.warehouse.manage.entity.vo.request.UnitSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.UnitSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.UnitVo;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.service.UnitService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 单位业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-09 19:21:26
 */
@Slf4j
@Service
public class UnitServiceImpl implements UnitService {

    @Autowired
    private UnitMapper mapper;

    @Override
    public Unit selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public UnitVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        Unit unit = this.selectById(id);
        AssertCommonUtil.notNull(unit, "此数据不存在或者已被删除");
        this.updateByPrimaryKeySelective(Unit.builder().id(id)
                .isDelete(DelFlagEnum.DELETED.value).build());
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        for (Long id : idList) {
            this.deleteById(id);
        }
    }

    @Override
    public Long insert(Unit unit) {
        Date now = new Date();
        unit.setCreateTime(now);
        unit.setUpdateTime(now);
        return mapper.insert(unit) > 0 ? unit.getId() : 0L;
    }

    @Override
    public Long insertSelective(Unit unit) {
        Date now = new Date();
        unit.setCreateTime(now);
        unit.setUpdateTime(now);
        return mapper.insertSelective(unit) > 0 ? unit.getId() : 0L;
    }

    @Override
    public boolean update(Unit unit) {
        Date now = new Date();
        unit.setUpdateTime(now);
        return mapper.updateByPrimaryKey(unit) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Unit record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(UnitSaveOrUpdateVo saveOrUpdateVo) {
        Integer count = this.count(UnitSearchVo.builder()
                .nameAccurate(saveOrUpdateVo.getName())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        count = this.count(UnitSearchVo.builder()
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        Unit unit = new Unit();
        BeanUtils.copyProperties(saveOrUpdateVo, unit);
        boolean b = this.insertSelective(unit) != 0;
        saveOrUpdateVo.setId(unit.getId());
        return b;
    }

    @Override
    public boolean modify(UnitSaveOrUpdateVo saveOrUpdateVo) {
        Unit old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        Integer count = this.count(UnitSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .nameAccurate(saveOrUpdateVo.getName())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        count = this.count(UnitSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        old.setName(saveOrUpdateVo.getName());
        old.setNumber(saveOrUpdateVo.getNumber());
        old.setNote(saveOrUpdateVo.getNote());
        old.setUpdateUserId(saveOrUpdateVo.getUpdateUserId());
        old.setUpdateUserName(saveOrUpdateVo.getUpdateUserName());
        old.setIsDelete(DelFlagEnum.USING.value);
        return this.update(old);
    }

    @Override
    public UnitVo selectOne(UnitSearchVo unitSearchVo) {
        return mapper.selectOne(unitSearchVo);
    }

    @Override
    public Integer count(UnitSearchVo unitSearchVo) {
        return mapper.count(unitSearchVo);
    }

    @Override
    public List<UnitVo> selectList(UnitSearchVo unitSearchVo) {
        return mapper.selectList(unitSearchVo);
    }

    @Override
    public Page<UnitVo> selectPage(UnitSearchVo unitSearchVo) {
        AssertCommonUtil.check(Objects.isNull(unitSearchVo.getPageNum()) || Objects.isNull(unitSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(unitSearchVo);
        //分页
        Page<UnitVo> page = new Page<>(true, count, unitSearchVo.getPageNum(), unitSearchVo.getPageSize());
        unitSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<UnitVo> responseVos = this.selectList(unitSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Unit> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}
